package com.rfid.rfidbluetooth;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RfidStream {

    private OutputStream mOutStream;
    private DataInputStream mInput;
    boolean close;

    public static short CMD_REQ_RFID = (short) 0XF005;

    public static short CMD_M_EPC = (short) 0XF006;


    public RfidStream(OutputStream mOutStream, InputStream input) {
        this.mOutStream = mOutStream;
        this.mInput = new DataInputStream(input);
    }

    public void reqRfids(int shelf,int pn) throws IOException {
        byte data[] = CmdBuild.reqRfid(shelf,pn);
        mOutStream.write(data);
        System.out.println("reqRfids");
    }

    public  List<Rfid> readRfids() throws IOException {
        System.out.println("readRfids");
        short cmd = mInput.readShort();
        if (cmd == CMD_REQ_RFID) {
            return CmdBuild.readRfids(mInput);
        }
        return null;
    }

    public boolean modifyEpc(int shelf,int ph,int port,String epc) throws Exception {
        byte data[] = CmdBuild.modifyEpc(shelf,ph,port,epc);
        mOutStream.write(data);

        short cmd = mInput.readShort();
        if (cmd != CMD_M_EPC) {
            throw  new IOException("通讯错误");
        }
//        if (shelf != mInput.read()) {
//            throw  new IOException("层架不一致");
//        }
//        if (ph != mInput.read()) {
//            throw  new IOException("盘号不一致");
//        }
//        if (port != mInput.read()) {
//            throw  new IOException("端口不一致");
//        }
//        int status = mInput.read();
//        int v = mInput.read();
//        return status == 0x01;
        return CmdBuild.readModifyEpcResualt(mInput);
    }


    private void writeRfids(int ph) throws IOException {
        ByteBuffer bb = ByteBuffer.allocate(4);
        short cmd = CMD_REQ_RFID;
        System.out.println("writeRfids cmd = " + Integer.toHexString(cmd&0XFFFF));
        bb.putShort(cmd);
        bb.put((byte) 1);
        bb.put((byte) ph);
        mOutStream.write(bb.array());
        for (int i=0;i<12;i++) {
            mOutStream.write(i+1);
            String epc = "ABC" + i;
            mOutStream.write(epc.getBytes().length);
            mOutStream.write(epc.getBytes());
        }
        //校验位
        mOutStream.write(0);
    }

    public void listenning() throws IOException {
        while (!close) {
            int cmd = mInput.readShort();
            System.out.println("listenning cmd = " + Integer.toHexString(cmd&0XFFFF));
            if (cmd == CMD_REQ_RFID) {
                int cell = mInput.read();
                int ph = mInput.read();
                int port = mInput.read();
                int len = mInput.read();
                byte buffer[] = new byte[len];
                if (len > 0) {
                    mInput.readFully(buffer,0,len);
                }
                int m = mInput.read();

                writeRfids(ph);
            }
        }
    }

    public void close() throws IOException {
        close = true;
        mInput.close();
        mOutStream.close();
    }
}
